<?php
namespace Api\Controller;
use Api\Controller\CommonController;
class WeixinPayController extends CommonController {
	public function _initialize(){
		parent::_initialize();
		 
		//登录验证
		$this->_checkLogin();
	}
	
	/**
	 *  统一下单
	 */
	public function unifiedorder(){
		$data=array(
    		"appid"=>I("post.pan"),
    		
		);
		
		if(!isNotEmptyArr($data)) $this->_returnJson(array("result"=>0,"info"=>"missing parameter","code"=>"123"));
		
		$url = C('WEIXINPAY_URL').'pay/unifiedorder';
		$result=http_post($url,);
    	
		if (!$result) $this->_returnJson(array("result"=>0,"info"=>"api return error","errorMsg"=>$this->apiErrorMsg,"code"=>"119") );
    	$this->_returnJson(array("result"=>1,"info"=>$result));
    }
    
    /**
     *  卡密码加密接口
     */
    public function getEnPwd(){
		$password = I("post.password");
    	if(empty($password)) $this->_returnJson(array("result"=>0,"info"=>"missing parameter","code"=>"123"));
    	$password=I("post.password");
    	
    	$str=$this->_getEncryptPwd($password);
    	$this->_returnJson(array("result"=>1,"encryptPwd"=>$str));
    }
    
    /**
     *  绑定汇通卡
     */
    public function bindCard(){
    	$data=array(
    			"pan"=>I("post.pan"),
    			"password"=>$_POST['password'],
    	);
    	
    	/* $data=array(
    			"pan"=>"6660718800405200123",
    			"password"=>"KSrGqSnIwsbDyu7iQ6ikZyDfWeU4ICeqf4NMWh76JZ4r2LLQgQxKNOMMm+0xVwNf7vigmhxv652PIdPsV2a66n914WCBMNLtbOoe9tLFjgZtzH7BgyxEUKp+IE4K5Sko9eOdsJdGZN/zwSwH1mzYHySFwbD04hBFk7In4mKdjYo=",
    	); */
    	
    	if(!isNotEmptyArr($data)) $this->_returnJson(array("result"=>0,"info"=>"missing parameter","code"=>"123"));
    	$result=$this->_getHuitongTokenID($data['pan'], $data['password']);
    	
    	if (!$result) $this->_returnJson(array("result"=>0,"info"=>"api error","errorMsg"=>$this->apiErrorMsg,"code"=>119));
    	
    	$user=$this->user;
    	
    	//存入数据库
    	$isCard=D("UserCard")->isCard($user['uid'],$data['pan']);
    	
    	//储存汇通卡
    	if (!$isCard) {
    		$addCard=D("UserCard")->addCard($user['uid'],$data['pan'],$data['password'],$result['tokenId']);
    	}
    	else{
    		$updateCard=D("UserCard")->save(array("cardID"=>$isCard['cardID'],"cardPassword"=>$data['password'],"tokenID"=>$result['tokenId'],"tokenExpires"=>time()+60*60*24*7,"updateTime"=>time()));
    	}
    	
    	//获取卡信息
    	$cardInfo=$this->_getCard("03",$result['tokenId']);
    	
    	$this->_returnJson(array("result"=>1,"pan"=>$cardInfo['pan'],"balance"=>$cardInfo['balance']));
    }
    
    /**
     *  汇通卡余额查询（批量）
     */
    public function getAllCard(){
		$allCard=D("UserCard")->userCardList($this->user['uid']);
		
		if (count($allCard)==0) $this->_returnJson(array("result"=>0,"info"=>"empty card","code"=>124));
		
		foreach ($allCard as $a){
			if($a['tokenExpires'] > time()){     //token未过期
			    $card[]=array(
			        "operType"=>"03",
			        "operValue"=>$a['tokenID']
			    );
			}
			else{
			    $result=$this->_getHuitongTokenID($a['cardNum'], $a['cardPassword']);
			    if (!$result) $this->_returnJson(array("result"=>0,"info"=>"api error","errorMsg"=>$this->apiErrorMsg,"code"=>119));
			    $updateCard=D("UserCard")->save(array("cardID"=>$a['cardID'],"cardPassword"=>$a['cardPassword'],"tokenID"=>$result['tokenId'],"tokenExpires"=>time()+60*60*24*7,"updateTime"=>time()));
			    $card[]=array(
			        "operType"=>"03",
			        "operValue"=>$result['tokenId']
			    );
			}
		}
		
		$str=array(
			"info"=>$card
		);
		
		$result=$this->_getAllCard($str['info']);
		
    	if (!$result) $this->_returnJson(array("result"=>0,"info"=>"api error","errorMsg"=>$this->apiErrorMsg,"code"=>119));
    	//dump($result);
    	foreach ($result as $key=>$a){
    		$cardList[$key]['pan']=$a['pan'];
    		$cardList[$key]['balance']=$a['balance'];
    		
    		if ($a['respCode']=="00000") {
    			$cardList[$key]['status']=1;
    		}
    		else{
    			$cardList[$key]['status']=0;
    			$cardList[$key]['errorMsg']=$a['respMsg'];
    		}
    	}
    	
    	$this->_returnJson(array("result"=>1,"info"=>$cardList));
    } 
    
    /**
     *  汇通卡消费明细
     */
    public function queryCardDetail(){
    	$data=array(
    			"pan"=>I("pan"),
    	);
    	
    	if(!isNotEmptyArr($data)) $this->_returnJson(array("result"=>0,"info"=>"missing parameter","code"=>"123"));
    	
    	//检测是否已绑定
    	$isCard=D("UserCard")->isCard($this->user['uid'],$data['pan']);
    	if (!$isCard) $this->_returnJson(array("result"=>0,"info"=>"missing card","code"=>"125"));
    	
    	$startDate=I("post.startDate");
    	$endDate=I("post.endDate");
    	$pageSize=intval(I("post.pageSize"));
    	$pageIndex=intval(I("post.pageIndex"));
    	
    	if (empty($pageSize)) $pageSize=10;
    	if (empty($pageIndex)) $pageIndex=1;
    	
    	$result=$this->_queryCardDetail($operType="03",$isCard['tokenID'],$pageSize,$pageIndex,"",$startDate,$endDate);
    	if (!$result) $this->_returnJson(array("result"=>0,"info"=>"api error","errorMsg"=>$this->apiErrorMsg,"code"=>119));
    	
    	//处理数据
    	foreach ($result['records'] as $a){
    		$records[]=array(
    			"merchantName"=>$a['merchantName'],
    			"amount"=>$a['amount'],
    			"transDate"=>$a['transDate']
    		);
    	}
    	
    	$return=array(
    		"pageSize"=>$result['pageSize'],
    		"totalRecords"=>$result['totalRecords'],
    		"pageIndex"=>$result['pageIndex'],
    		"records"=>$records
    	);
    	
    	$this->_returnJson(array("result"=>1,"info"=>$return));
    }
    
}